package com.zyg.predicate;

import com.zyg.pojo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class MyPredicateDemo {
    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        users.add(new User(1,"张三",18));
        users.add(new User(2,"李四",20));
        users.add(new User(3,"王五",19));
        users.add(new User(4,"赵六",18));
        //List<User> filter = filter(users, s -> s.getAge() == 19 ,s->s.getName().equals("张三"));
        //List<User> filter = filter1(users, s -> s.getAge() == 18 ,s->s.getName().equals("张三"));
        List<User> filter = filter2(users, s -> s.getAge() == 18 ,s->s.getName().equals("张三"));
        System.out.println(filter);
    }

    public static List<User> filter1(List<User> users, Predicate<User> userPredicate,Predicate<User> userPredicate2) {
        List<User> userList = new ArrayList<>();
        users.forEach(user -> {
            if (userPredicate.and(userPredicate2).test(user)) {
                userList.add(user);
            }
        });
        return userList;
    }

    public static List<User> filter2(List<User> users, Predicate<User> userPredicate,Predicate<User> userPredicate2) {
        List<User> userList = new ArrayList<>();
        users.forEach(user -> {
            if (userPredicate.and(userPredicate2.negate()).test(user)) {
                userList.add(user);
            }
        });
        return userList;
    }
}
